package cn.schoolwow.flow.monitor.module.business.configParameter.config;

import cn.schoolwow.ams.domain.annotation.AMSFieldLabel;
import cn.schoolwow.flow.monitor.annatation.ConfigParameterBean;
import cn.schoolwow.flow.monitor.domain.FlowMonitorOption;
import cn.schoolwow.flow.monitor.entity.ConfigParameter;
import cn.schoolwow.quickdao.domain.database.dml.CompareInstanceListResult;
import cn.schoolwow.util.domain.query.reflection.QueryReflection;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class ConfigParameterInitializer implements InitializingBean {
    @Autowired
    private FlowMonitorOption flowMonitorOption;

    @Autowired
    private ApplicationContext applicationContext;

    @Override
    public void afterPropertiesSet() throws Exception {
        Map<String,Object> beanMap = applicationContext.getBeansWithAnnotation(ConfigParameterBean.class);
        if(beanMap.isEmpty()){
            flowMonitorOption.dao.clear(ConfigParameter.class);
            return;
        }

        List<ConfigParameter> configParameterList = new ArrayList<>();
        for(Map.Entry<String,Object> entry:beanMap.entrySet()){
            String beanName = entry.getKey();
            Object instance = entry.getValue();

            ConfigParameter configParameter = new ConfigParameter();
            {
                AMSFieldLabel amsFieldLabel = instance.getClass().getAnnotation(AMSFieldLabel.class);
                if(null==amsFieldLabel){
                    configParameter.setName(instance.getClass().getSimpleName());
                }else{
                    configParameter.setName(amsFieldLabel.value());
                }
            }
            configParameter.setClassName(instance.getClass().getName());
            configParameter.setBeanName(beanName);
            configParameter.setValue(JSON.parseObject(JSON.toJSONString(instance)));
            List<Field> fieldList = QueryReflection.newQuery(instance.getClass())
                    .execute()
                    .getFieldList();
            StringBuilder builder = new StringBuilder();
            for(Field field:fieldList){
                AMSFieldLabel amsFieldLabel = field.getAnnotation(AMSFieldLabel.class);
                if(null==amsFieldLabel){
                    builder.append(field.getName()+":暂无说明\r\n");
                }else{
                    builder.append(field.getName()+":"+amsFieldLabel.value()+"\r\n");
                }
            }
            configParameter.setEnable(1);
            configParameter.setComment(builder.toString());
            configParameterList.add(configParameter);
        }
        List<ConfigParameter> list = flowMonitorOption.dao.query(ConfigParameter.class).execute().getList();
        CompareInstanceListResult<ConfigParameter> compareInstanceListResult = flowMonitorOption.dao.compareInstanceList(configParameterList,list);
        flowMonitorOption.dao.insert(compareInstanceListResult.insertInstanceList);
        flowMonitorOption.dao.delete(compareInstanceListResult.deleteInstanceList);
    }
}
